package cn.flying.cloud.system.view;


import javax.annotation.Resource;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.base.common.vo.layui.TreeTableInfoVo;
import cn.flying.cloud.base.common.vo.layui.XmSelect;
import cn.flying.cloud.system.common.service.CodeService;
import cn.flying.cloud.system.common.vo.OrgTreeTableInfo;
import cn.flying.cloud.system.org.entity.SysOrgInfo;
import cn.flying.cloud.system.org.service.SysOrgInfoService;
import cn.flying.cloud.system.service.SysOrganizationService;

/**
 * @description: 组织机构管理
 * @author: admin
 * @date: 2022年05月22日 17:58
 * @version: 1.0
 */
@Controller
@RequestMapping("/sys/organization/")
public class SysOrganizationViewController {

    @Resource
    private SysOrganizationService sysOrganizationService;
    @Resource
    private SysOrgInfoService sysOrgInfoService;
    @Resource
    private CodeService codeService;

    /**
     * 列表页面
     *
     * @return
     */
    @RequestMapping("index")
    public String list(Model model) {
        Map<String, String> orgTypes = codeService.getCodeList("org_type");
        model.addAttribute("orgTypes", orgTypes);
        Map<String, String> orgLevels = codeService.getCodeList("org_level");
        model.addAttribute("orgLevels", orgLevels);
        Map<String, String> statusList = codeService.getCodeList("status");
        model.addAttribute("statusList", statusList);
        return "sys/organization/index";
    }

    /**
     * 加载资源treetable
     *
     * @return
     */
    @RequestMapping("getTreeTableList")
    @ResponseBody
    public TreeTableInfoVo getOrganizationTreeTableList(SysOrgInfo orgInfo) {
        List<OrgTreeTableInfo> list = sysOrganizationService.getTreeTableList(orgInfo);
        return new TreeTableInfoVo(0, "成功", list.size(), list);
    }

    /**
     * xm-select 组织下拉框数据
     *
     * @return
     */
    @PostMapping(value = "getSelectList")
    @ResponseBody
    public Rt<List<XmSelect>> getSelectList() {
        List<XmSelect> treeList = sysOrganizationService.getSelectList();
        return Rt.success(treeList);
    }

    /**
     * 新增/修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("turn/edit")
    public String turnEdit(Long id, String orgId, Model model) {
        Map<String, String> orgTypes = codeService.getCodeList("org_type");
        model.addAttribute("orgTypes", orgTypes);
        Map<String, String> orgLevels = codeService.getCodeList("org_level");
        model.addAttribute("orgLevels", orgLevels);
        if (id != null) {
            SysOrgInfo organization = sysOrgInfoService.getById(id);
            model.addAttribute("organization", organization);
        } else {
            SysOrgInfo organization = new SysOrgInfo();
            organization.setParentId(orgId);
            model.addAttribute("organization", organization);
        }
        return "sys/organization/edit";
    }
}
